#include "dlg_book_getandset.h"
#include "ui_dlg_book_getandset.h"
#include "lib/sqlmgr.h"
Dlg_Book_GetAndSet::Dlg_Book_GetAndSet(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_Book_GetAndSet)
{
    ui->setupUi(this);
}

Dlg_Book_GetAndSet::~Dlg_Book_GetAndSet()
{
    delete ui;
}

void Dlg_Book_GetAndSet::setType(bool isReturn)
{
    m_isReturn = isReturn;
    ui->btn_get->setText("归还");
}

void Dlg_Book_GetAndSet::setBookId(int id)
{
    m_bookId = id;
}

void Dlg_Book_GetAndSet::on_btn_cancel_clicked()
{
    this->done(0);
}


void Dlg_Book_GetAndSet::on_btn_get_clicked()
{
    // if(m_isReturn) ui->btn_get->setText("归还");
    do{
        QString strName = ui->le_user->text();
        QString strPassword = ui->le_password->text();
        int userId;
        bool success = SqlMgr::getInstance()->login(strName,strPassword,userId);
        if(!success){
            this->done(0);
            break;
        }
        if(m_isReturn){
            SqlMgr::getInstance()->backBook(QString::number(userId),QString::number(m_bookId));


        }else{
            SqlMgr::getInstance()->borrowBook(QString::number(userId),QString::number(m_bookId));
        }
        this->done(1);
    }while(false);
}

